package com.itita.ww2.redis.core;

/**
 * 单点Redis配置
 * @author NorthLan
 *
 */
public class RedisConfig {
	// redis
	private String masterHost;
	private String masterPwd;
	private int masterPort;
	private int updateInterval;
	// config
	private int poolMaxIdle;
	private int poolMaxTotal;
	private int poolMaxWaitMillis;
	private boolean poolTestOnBorrow;
	private boolean poolTestOnReturn;
	private boolean jmxEnable;
	// mysql
	private String dbDriverName;
	private String dbConnectionUrl;
	private String dbUsername;
	private String dbPassword;
	// 
	private int batchCount = 500;

	private RedisConfig(Builder b) {
		this.masterHost = b.masterHost;
		this.masterPwd = b.masterPwd;
		this.masterPort = b.masterPort;

		this.updateInterval = b.updateInterval;
		this.poolMaxIdle = b.poolMaxIdle;
		this.poolMaxTotal = b.poolMaxTotal;
		this.poolMaxWaitMillis = b.poolMaxWaitMillis;
		this.poolTestOnBorrow = b.poolTestOnBorrow;
		this.poolTestOnReturn = b.poolTestOnReturn;
		this.jmxEnable = b.jmxEnable;

		this.dbDriverName = b.dbDriverName;
		this.dbConnectionUrl = b.dbConnectionUrl;
		this.dbUsername = b.dbUsername;
		this.dbPassword = b.dbPassword;

		this.batchCount = b.batchCount;
	}

	public final String getMasterHost() {
		return masterHost;
	}

	public final String getMasterPwd() {
		return masterPwd;
	}

	public final int getMasterPort() {
		return masterPort;
	}

	public final int getUpdateInterval() {
		return updateInterval;
	}

	public final int getPoolMaxIdle() {
		return poolMaxIdle;
	}

	public final int getPoolMaxTotal() {
		return poolMaxTotal;
	}

	public final int getPoolMaxWaitMillis() {
		return poolMaxWaitMillis;
	}

	public final boolean isPoolTestOnBorrow() {
		return poolTestOnBorrow;
	}

	public final boolean isPoolTestOnReturn() {
		return poolTestOnReturn;
	}

	public final boolean isJmxEnable() {
		return jmxEnable;
	}

	public final String getDbDriverName() {
		return dbDriverName;
	}

	public final String getDbConnectionUrl() {
		return dbConnectionUrl;
	}

	public final String getDbUsername() {
		return dbUsername;
	}

	public final String getDbPassword() {
		return dbPassword;
	}

	public final int getBatchCount() {
		return batchCount;
	}

	public static class Builder {
		// redis
		private String masterHost;
		private String masterPwd;
		private int masterPort;
		private int updateInterval;
		// config
		private int poolMaxIdle;
		private int poolMaxTotal;
		private int poolMaxWaitMillis;
		private boolean poolTestOnBorrow;
		private boolean poolTestOnReturn;
		private boolean jmxEnable;
		// mysql
		private String dbDriverName;
		private String dbConnectionUrl;
		private String dbUsername;
		private String dbPassword;
		//
		private int batchCount;

		public Builder(String host, String password, int port) {
			this.masterHost = host;
			this.masterPwd = password;
			this.masterPort = port;
		}

		public Builder setUpdateInterval(int interval) {
			this.updateInterval = interval;
			return this;
		}

		public Builder setPoolMaxAttr(int maxIdle, int maxTotal) {
			this.poolMaxIdle = maxIdle;
			this.poolMaxTotal = maxTotal;

			return this;
		}

		public Builder setPoolMaxWaitMillis(int maxWaitMillis) {
			this.poolMaxWaitMillis = maxWaitMillis;
			return this;
		}

		public Builder setPoolTest(boolean onBorrow, boolean onReturn) {
			this.poolTestOnBorrow = onBorrow;
			this.poolTestOnReturn = onReturn;
			return this;
		}

		public Builder setJmxEnable(boolean enable) {
			this.jmxEnable = enable;
			return this;
		}

		public Builder setMysql(String driverName, String connectionUrl, String username, String password) {
			this.dbDriverName = driverName;
			this.dbConnectionUrl = connectionUrl;
			this.dbUsername = username;
			this.dbPassword = password;
			return this;
		}

		public Builder setBatchCount(int count) {
			this.batchCount = count;
			return this;
		}

		public RedisConfig build() {
			return new RedisConfig(this);
		}

	}
}
